﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类t_Contract_Attachment。
    /// </summary>
    public class ContractAttachment
    {
        public ContractAttachment()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Contract_Attachment where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.ContractAttachment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Contract_Attachment(");
            strSql.Append("RecordId,RelationId,Path,FileName,Remark,UserId,UserName,DepartId,DepartName,CompanyId,CompanyName,CityId,CityName,AttType,AddTime)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@RelationId,@Path,@FileName,@Remark,@UserId,@UserName,@DepartId,@DepartName,@CompanyId,@CompanyName,@CityId,@CityName,@AttType,@AddTime)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "RelationId", DbType.Guid, model.RelationId);
            db.AddInParameter(dbCommand, "Path", DbType.AnsiString, model.Path);
            db.AddInParameter(dbCommand, "FileName", DbType.AnsiString, model.FileName);
            db.AddInParameter(dbCommand, "Remark", DbType.AnsiString, model.Remark);
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartName", DbType.AnsiString, model.DepartName);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "AttType", DbType.Int32, model.AttType);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.ContractAttachment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Contract_Attachment set ");
            strSql.Append("RelationId=@RelationId,");
            strSql.Append("Path=@Path,");
            strSql.Append("FileName=@FileName,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("UserId=@UserId,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("DepartId=@DepartId,");
            strSql.Append("DepartName=@DepartName,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("CityName=@CityName,");
            strSql.Append("AttType=@AttType,");
            strSql.Append("AddTime=@AddTime");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "RelationId", DbType.Guid, model.RelationId);
            db.AddInParameter(dbCommand, "Path", DbType.AnsiString, model.Path);
            db.AddInParameter(dbCommand, "FileName", DbType.AnsiString, model.FileName);
            db.AddInParameter(dbCommand, "Remark", DbType.AnsiString, model.Remark);
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartName", DbType.AnsiString, model.DepartName);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "AttType", DbType.Int32, model.AttType);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Contract_Attachment ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractAttachment GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,RelationId,Path,FileName,Remark,UserId,UserName,DepartId,DepartName,CompanyId,CompanyName,CityId,CityName,AttType,AddTime from t_Contract_Attachment ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Business.Entity.ContractAttachment model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,RelationId,Path,FileName,Remark,UserId,UserName,DepartId,DepartName,CompanyId,CompanyName,CityId,CityName,AttType,AddTime ");
            strSql.Append(" FROM t_Contract_Attachment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "t_Contract_Attachment");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.ContractAttachment> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,RelationId,Path,FileName,Remark,UserId,UserName,DepartId,DepartName,CompanyId,CompanyName,CityId,CityName,AttType,AddTime ");
            strSql.Append(" FROM t_Contract_Attachment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.ContractAttachment> list = new List<Chenxin.Lib.Business.Entity.ContractAttachment>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractAttachment ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.ContractAttachment model = new Chenxin.Lib.Business.Entity.ContractAttachment();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["RelationId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RelationId = new Guid(ojb.ToString());
            }
            model.Path = dataReader["Path"].ToString();
            model.FileName = dataReader["FileName"].ToString();
            model.Remark = dataReader["Remark"].ToString();
            ojb = dataReader["UserId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UserId = new Guid(ojb.ToString());
            }
            model.UserName = dataReader["UserName"].ToString();
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            model.DepartName = dataReader["DepartName"].ToString();
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            model.CityName = dataReader["CityName"].ToString();
            ojb = dataReader["AttType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AttType = (int)ojb;
            }
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

